﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Thread = System.Threading.Tasks;
using Microsoft.Practices.Unity;
using Infrastucture;
using TimeSplicerModule.Factories;
using TimeSplicerModule.DomainModels;
using TimeSplicerModule.Security;
using TimeSplicerModule.ViewModels;

namespace TimeSplicerModule.Services
{
    public interface ITaskScheduleService
    {
        TaskViewModel CreateScheduleForTask(TaskViewModel task);
        TaskScheduleModel GetSchedule(TaskViewModel task);
        bool StartScheduledTask(TaskViewModel task);
        bool CompleteSheduledTask(TaskViewModel task);
    }

    public class TaskScheduleService : ITaskScheduleService
    {
        private readonly ITaskScheduleFactory _scheduleFactory;
        private readonly IScheduledTaskFactory _scheduledTaskFactory;

        public TaskScheduleService(ITaskScheduleFactory scheduleFactory, IScheduledTaskFactory scheduledTaskFactory)
        {
            _scheduleFactory = scheduleFactory;
            _scheduledTaskFactory = scheduledTaskFactory;
        }

        public TaskViewModel CreateScheduleForTask(TaskViewModel task)
        {
            if (task.IsRecurring)
            {
                // update schedule - for now not going to care if its from the original or not, will
                // allow all derived versions to update the schedule

                // need to make sure all relevent info is set
                if (!task.IsDerived)
                    task.Schedule.TaskGuid = task.TaskGuid;
                else
                    task.Schedule.TaskGuid = task.DerivedFromTaskGuid;

                task.Schedule.IsActive = true;

         
                _scheduleFactory.Upsert(task.Schedule);
            }
            else if (task.Schedule.IsActive)
            {
                // if taks is no longer reoccuring and scedule is active
                // deactivate 
                task.Schedule.IsActive = false;
                _scheduleFactory.Upsert(task.Schedule);
            }

            return task;
        }

        public TaskScheduleModel GetSchedule(TaskViewModel task)
        {
            // TODO: create new one if returns null
            TaskScheduleModel schedule = null;

            if (task.IsDerived)
            {
                schedule = _scheduleFactory.Get(x => x.TaskGuid == task.DerivedFromTaskGuid);
            }
            else
            {
                schedule = _scheduleFactory.Get(x => x.TaskGuid == task.TaskGuid);
            }

            if (schedule == null)
            {
                schedule = new TaskScheduleModel
                {
                    OccuranceType = Helpers.TaskScheduleOccuranceTypes.Weekly,
                    StartDate = DateTime.Today,
                    StartHour = 8,
                    StartAmPm = Helpers.TimeOfDay.AM,
                    EndHour = 5,
                    EndAmPm = Helpers.TimeOfDay.PM,
                    EndDate = DateTime.Today,
                    IncludeMonday = true,
                    IncludeTuesday = true,
                    IncludeWednesday = true,
                    IncludeThursday = true,
                    IncludeFriday = true
                };


                if (task.IsDerived)
                    schedule.TaskGuid = task.DerivedFromTaskGuid;
                else
                    schedule.TaskGuid = task.TaskGuid;
            }

            return schedule;
        }


        public bool StartScheduledTask(TaskViewModel task)
        {
            bool updated = false;

            if (task.IsRecurring)
            {
                if (task.Schedule == null)
                    task.Schedule = _scheduleFactory.Get(x => x.TaskGuid == task.TaskGuid);

                var scheduled = _scheduledTaskFactory.Get(x => x.ScheduleGuid == task.Schedule.ScheduleGuid);

                if (scheduled != null && scheduled.StartDate == DateTime.MinValue)
                {
                    // then update to tasks start date 
                    scheduled.StartDate = task.StartedDate;

                    scheduled = _scheduledTaskFactory.Upsert(scheduled);
                    updated = true;
                }
            }

            return updated;
        }

        public bool CompleteSheduledTask(TaskViewModel task)
        {
            bool updated = false;

            if (task.IsRecurring)
            {
                if (task.Schedule == null)
                    task.Schedule = _scheduleFactory.Get(x => x.TaskGuid == task.Schedule.TaskGuid);

                var scheduled = _scheduledTaskFactory.Get(x => x.ScheduleGuid == task.Schedule.ScheduleGuid);

                if (scheduled != null && !scheduled.IsCompleted)
                {
                    // then complete the scheduled task
                    scheduled.IsCompleted = true;
                    scheduled.CompletedDate = task.CompletedDate;

                    scheduled = _scheduledTaskFactory.Upsert(scheduled);

                    updated = true;
                }
            }

            return updated;
        }
    }
}
;